package com.neutech.maplestory.entity;

import com.neutech.maplestory.client.MapleStoryClient;
import com.neutech.maplestory.constant.Constant;
import com.neutech.maplestory.util.ImageUtil;
import com.neutech.maplestory.util.MyMusicThread;

import java.awt.*;
import java.util.List;
import java.util.Random;

/**
 * @author ZZQ
 * @PackageName: com.neutech.maplestory.entity
 * @ClassName: Arrow
 * @Description:
 * @date 2021/1/28 18:42
 */
public class Arrow extends AbstractMapleStoryObject{

    public static Image[]imgs = {
            ImageUtil.getImage("hero_right_arrow"),
            ImageUtil.getImage("hero_left_arrow"),
    };
    //public Image img;

//    /**
//     * 表示生死变量
//     */
//    public boolean live;

    public Arrow(){
        this.speed = 10;
        this.live = true;
        this.width = imgs[0].getWidth(null);
        this.height = imgs[1].getHeight(null);
    }

    public Arrow(MapleStoryClient msc,int x, int y, Direction dir){
        this();
        this.msc = msc;
        this.x = x;
        this.y = y;
        this.dir = dir;
//        if(dir == Direction.RIGHT){
//            this.img = imgs[0];
//        }else{
//            this.img = imgs[1];
//        }
    }

    @Override
    public void draw(Graphics g) {
        if(!live){
            msc.arrows.remove(this);
            return;
        }
        switch (dir){
            case RIGHT:
                g.drawImage(imgs[0],x,y,null);
                break;
            case LEFT:
                g.drawImage(imgs[1],x,y,null);
                break;
            default:
                break;
        }
        move();
    }

    @Override
    public void move() {
        switch (dir){
            case RIGHT:
                this.x += speed;
                break;
            case LEFT:
                this.x -= speed;
                break;
            default:
                break;
        }
        outOfBound();
    }

    /**
     * 边界出界判断
     */
    private void outOfBound(){
        if (this.x < -500 || this.x > Constant.GAME_WIDTH + 500){
            this.live = false;
        }
    }

    /**
     * 随机生成器
     */
    public static Random r = new Random();
    /**
     * 弓箭hit 怪物的方法
     * @param mob 被击中的怪
     * @return 是否击中
     */
    private boolean hit(Mob mob){
        if(this.live && this.getRectangle().intersects(mob.getRectangle()) && mob.action != Action.DIE){
//            System.out.println("打到了");
            this.live = false;

//            msc.hero.hitValue = r.nextInt(msc.hero.ATT - 5) + 8;//取值范围8~12

            msc.hero.hitValue = (int) (msc.hero.ATT *(1 + r.nextDouble()));//1~2倍
//            mob.HP -= msc.hero.hitValue;

            //创建出伤害值
            Power power = new Power(msc,mob.x,mob.y);
            msc.powers.add(power);

            mob.HP -= power.value;

            if(mob.HP <= 0){
            mob.action = Action.DIE;

            msc.hero.Experience += 500;
            if (msc.hero.Experience == msc.hero.MAX_Experience){
                msc.hero.Experience = 0;
                msc.hero.MAX_HP += 0.1 * msc.hero.MAX_HP;
                msc.hero.HP = msc.hero.MAX_HP;
                msc.hero.MAX_MP += 0.1 * msc.hero.MAX_MP;
                msc.hero.MP = msc.hero.MAX_MP;
                msc.hero.hitValue += 0.2 * msc.hero.hitValue;
            }

            new MyMusicThread("1.mp3", true).start();

            if(r.nextInt(100) < 80){
                Item item = new Item(msc,mob.x + mob.width / 2 - 14,mob.y + mob.height - 20,r.nextInt(4));
                msc.items.add(item);
                }
//            Item item = new Item(msc,mob.x + mob.width / 2 - 14,mob.y + mob.height - 20,r.nextInt(4));
//            msc.items.add(item);
            }
//            mob.live = false;
            return true;
        }
        return false;
    }

    /**
     * 一只弓箭与所有的怪进行碰撞检测的方法
     * @param mobs 怪物容器
     * @return 是否击中
     */
    public boolean hit (List<Mob>mobs){
        for (int i = 0; i < mobs.size(); i++) {
            Mob mob = mobs.get(i);
            if(hit(mob)){
                return true;
            }
        }
        return false;
    }
}


